#include <QApplication>
#include <QDesktopWidget>

#include <QtnRibbonDef.h>

#include "aboutdialog.h"
#include "ribbonwindow.h"

#define Font_Default        "System Default"
#define Font_Normal         "Normal"
#define Font_Large          "Large"
#define Font_ExLarge        "Extra Large"
#define DPI_100_percent     "DPI 100%"


/* RibbonWindow */
RibbonWindow::RibbonWindow(QWidget* parent)
    : Qtitan::RibbonMainWindow(parent)
    , m_ribbonStyle(qobject_cast<Qtitan::RibbonStyle*>(qApp->style()))
{
//    m_ribbonStyle->setDPIAware(false);
    RibbonToolTip::setWrapMode(RibbonToolTip::NativeWrap);
    createActions();
    //ribbonBar()->setFrameThemeEnabled();
    ribbonBar()->setTitleBarVisible(false);

    QAction* actionAbout = ribbonBar()->addAction(QIcon(":/res/about.png"), tr("关于"), Qt::ToolButtonIconOnly);
    actionAbout->setToolTip(tr("显示程序信息"));
    connect(actionAbout, SIGNAL(triggered()), this, SLOT(about()));

    m_actionRibbonMinimize = ribbonBar()->addAction(QIcon(":/res/ribbonMinimize.png"), tr("最小化工具栏"), Qt::ToolButtonIconOnly);
    m_actionRibbonMinimize->setStatusTip(tr("工具栏仅显示标签"));
    m_actionRibbonMinimize->setShortcut(tr("Ctrl+F1"));
    connect(m_actionRibbonMinimize, SIGNAL(triggered()), this, SLOT(maximizeToggle()));
    connect(ribbonBar(), SIGNAL(minimizationChanged(bool)), this, SLOT(minimizationChangedIcon(bool)));

    m_actionRibbonMinimizeMenu = new QAction(tr("最小化工具栏"), this);
    m_actionRibbonMinimizeMenu->setCheckable(true);
    connect(m_actionRibbonMinimizeMenu, SIGNAL(triggered()), this, SLOT(maximizeToggle()));
    connect((QWidget*)ribbonBar()->quickAccessBar(), SIGNAL(showCustomizeMenu(QMenu*)), this, SLOT(showQuickAccessCustomizeMenu(QMenu*)));
    connect(ribbonBar(), SIGNAL(showRibbonContextMenu(QMenu*, QContextMenuEvent*)), this, SLOT(showRibbonContextMenu(QMenu*, QContextMenuEvent*)));
}

void RibbonWindow::about()
{
	AboutDialog *msgBox = new AboutDialog(this, tr("三维构件设计"), "1.0 beta");
	msgBox->exec();
}

void RibbonWindow::setTitleGroupsVisible(bool checked)
{
    ribbonBar()->setTitleGroupsVisible(checked);
}

void RibbonWindow::maximizeToggle()
{
    ribbonBar()->setMinimized(!ribbonBar()->isMinimized());
}

void RibbonWindow::minimizationChangedIcon(bool minimized)
{
    m_actionRibbonMinimize->setChecked(minimized);
    m_actionRibbonMinimizeMenu->setChecked(minimized);
    m_actionRibbonMinimize->setIcon(minimized ? QIcon(":/res/ribbonMaximize.png") :  
        QIcon(":/res/ribbonMinimize.png"));
}

void RibbonWindow::showQuickAccessCustomizeMenu(QMenu* menu)
{
    menu->addSeparator()->setText(tr("常用自定义"));
    menu->addAction(ribbonBar()->quickAccessBarPosition() == RibbonBar::QATopPosition ? m_actionShowBelowRibbon : m_actionShowAboveRibbon);
    menu->addSeparator();
    menu->addAction(m_actionRibbonMinimizeMenu);
}

void RibbonWindow::switchQuickAccessBarPosition()
{
    ribbonBar()->setQuickAccessBarPosition(ribbonBar()->quickAccessBarPosition() == RibbonBar::QATopPosition ? 
        RibbonBar::QABottomPosition : RibbonBar::QATopPosition);
}

void RibbonWindow::showRibbonContextMenu(QMenu* menu, QContextMenuEvent* event)
{
    Q_UNUSED(menu)
    event->setAccepted(false);
}

#ifdef Q_OS_WIN
void RibbonWindow::frameTheme(bool checked)
{
    ribbonBar()->setFrameThemeEnabled(checked);
}

void RibbonWindow::createActions()
{
    m_actionShowBelowRibbon = new QAction(tr("在工具栏下面显示"), this);
    m_actionShowAboveRibbon = new QAction(tr("在工具栏上面显示"), this);
    connect(m_actionShowBelowRibbon, SIGNAL(triggered()), this, SLOT(switchQuickAccessBarPosition()));
    connect(m_actionShowAboveRibbon, SIGNAL(triggered()), this, SLOT(switchQuickAccessBarPosition()));
}
#endif // Q_WIN

